const fetch = (data, time) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve(data)
        }, time);
    })
}


function getPromise() {
    return new Promise(res =>
        res('success')
    )
}

getPromise().then(res => {
    console.log(res)
})

/**
 * async
 */


var a = 0
async function foo() {
    a = a + 2
    console.log(123)
    a = await a + 10
    console.log(a, 'a')
    return a
}
foo().then(res => {
    console.log(res, 23)
})
a = 3
console.log(a)

/**
 * async await解决callback hell
 *  */

async function foo2() {
    const result = []
    result[0] = await fetch(123, 1000)
    result[1] = await fetch(234, 1000)
    result[2] = await fetch(345, 1000)
    return result
}

foo2().then(res => {
    console.log(res)
})
